package com.haixiaoke.saas.callback;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.haixiaoke.saas.common.utils.GsonUtil;
import com.haixiaoke.saas.common.weChat.domain.ParseParam;
import com.haixiaoke.saas.common.weChat.service.WeChatService;
import com.haixiaoke.saas.common.weChat.util.WeChatUtils;
import com.haixiaoke.saas.department.domain.WechatAccount;
import com.haixiaoke.saas.department.service.IWechatAccountService;
import com.haixiaoke.saas.system.framework.manager.AsyncManager;
import com.haixiaoke.saas.system.framework.manager.factory.AsyncFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author qixi
 * @date 2024/9/5
 */
@RestController
@RequestMapping("/callback/workOrder")
@Slf4j
public class WorkOrderCallBackController {

    @Autowired
    private WeChatService weChatService;
    @Autowired
    private IWechatAccountService wechatAccountService;
    @PostMapping("/native/pay")
    public String callback(@RequestBody ParseParam parseParam) {
        try {
            log.info("微信扫码支付回调参数：{}", GsonUtil.toJson(parseParam));
            WechatAccount account = wechatAccountService.selectWechatAccountByTenantId(parseParam.getAttach());
            // 签名验证
            if (!weChatService.signCheck(parseParam, account.getAccountApiKey())) {
                log.info("扫码支付签名验证失败：查询的商户id：{}，回调的工单单id：{}，订单的组织id：{}", GsonUtil.toJson(account), parseParam.getOutTradeNo(), parseParam.getAttach());
                return parseParam.resultFail(WeChatUtils.FAIL, "签名失败");
            }
            // 异步处理回调业务
            AsyncManager.me().execute(AsyncFactory.weChatCallbackWorkOrderNativePay(parseParam));
        } catch (Exception e) {
            log.error("工单扫码支付回调发生了错误：{}", e.getLocalizedMessage(), e);
            return parseParam.resultFail(WeChatUtils.FAIL, e.getLocalizedMessage());
        }
        return parseParam.resultSuccess();
    }
    @PostMapping("/unified/pay")
    public String unifiedPay(@RequestBody ParseParam parseParam) {
        try {
            log.info("微信扫码支付回调参数：{}", GsonUtil.toJson(parseParam));
            WechatAccount account = wechatAccountService.selectWechatAccountByTenantId(parseParam.getAttach());
            // 签名验证
            if (!weChatService.signCheck(parseParam, account.getAccountApiKey())) {
                log.info("签名验证失败：查询的商户id：{}，回调的工单id：{}，订单的组织id：{}", GsonUtil.toJson(account), parseParam.getOutTradeNo(), parseParam.getAttach());
                return parseParam.resultFail(WeChatUtils.FAIL, "签名失败");
            }
            // 异步处理回调业务
            AsyncManager.me().execute(AsyncFactory.weChatCallbackWorkOrderUnifiedPay(parseParam));
        } catch (Exception e) {
            log.error("工单小程序支付回调发生了错误：{}", e.getLocalizedMessage(), e);
            return parseParam.resultFail(WeChatUtils.FAIL, e.getLocalizedMessage());
        }
        return parseParam.resultSuccess();
    }



}
